package artid.com.arttogo.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;

import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.CollecterActivity;
import artid.com.arttogo.bean.Follow;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.utils.NetUtils;
import artid.com.arttogo.utils.VeDate;
import artid.com.arttogo.view.DynamicHeightImageView;

public class AttentionDyncAdapter extends BaseAdapter {
	private Context mContext;
	LayoutInflater mInflater;
	ViewHolder holder;
	public List<Follow> mList;
	private ImageLoader mImageLoader;

	public AttentionDyncAdapter(Context mContext, List<Follow> bean) {
		this.mContext = mContext;
		mInflater = LayoutInflater.from(mContext);
		this.mList = bean;
		RequestQueue mQueue = Volley.newRequestQueue(mContext);
		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}
		
	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	public void addData(List<Follow> list){
		mList.addAll(list);
		notifyDataSetChanged();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_artwork_attention_dyn,
					null);
			vh.name = (TextView) convertView
					.findViewById(R.id.item_attention_name);
			vh.detail = (TextView) convertView
					.findViewById(R.id.item_attention_det);
			vh.time = (TextView) convertView
					.findViewById(R.id.item_attention_time);
			vh.zan = (TextView) convertView.findViewById(R.id.text_attention_z);
			vh.comment = (TextView) convertView
					.findViewById(R.id.text_attention_pl);
			vh.share = (TextView) convertView
					.findViewById(R.id.text_attention_share);
			vh.pic = (DynamicHeightImageView)convertView.findViewById(R.id.item_attention_main_img);
			vh.auth = (ImageView)convertView.findViewById(R.id.isauth);
			vh.icon = (ImageView)convertView.findViewById(R.id.item_attention_icon);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		final Follow itemFollow = mList.get(position);
		vh.name.setText(itemFollow.getArtistname());
		vh.detail.setText(itemFollow.getArtcontent());
		vh.zan.setText("赞(" + itemFollow.getPraise() + ")");
		vh.comment.setText("评论(" + itemFollow.getComment() + ")");
		vh.time.setText(VeDate.getStrMDTime(itemFollow.getTime()));
		if (itemFollow.getIs_auth().equals("2")) {
			vh.auth.setVisibility(View.VISIBLE);
		}else {
			vh.auth.setVisibility(View.GONE);
		}
		ImageListener listener = ImageLoader
				.getImageListener(vh.icon, R.drawable.ic_menu_rotate,
						R.drawable.ic_menu_rotate);
		mImageLoader.get(itemFollow.getArtistimgurl(), listener);
		
//		ImageListener listener2 = ImageLoader
//				.getImageListener(vh.pic, R.drawable.ic_menu_rotate,
//						R.drawable.ic_menu_rotate);
//		mImageLoader.get(itemFollow.getImgurl()+ AppUtil.picWidthUrl, listener2);
		vh.pic.setHeight(itemFollow.getHeight());
		Glide.with(mContext).load(itemFollow.getImgurl()
				+ "_"+ (Integer.parseInt(NetUtils.getDisplayMetrics((Activity)mContext).get(NetUtils.DM_WIDTH))) + "x10.png")
		.placeholder(R.drawable.ic_menu_rotate)
		.error(R.drawable.ic_menu_rotate).into(vh.pic);

		vh.share.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				AppUtil.showShare(mContext, itemFollow.getArttitle(), itemFollow.getArtcontent(), itemFollow.getImgurl(), itemFollow.getImgurl());
			}
		});
		vh.icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext,CollecterActivity.class);
				intent.putExtra("id", itemFollow.getArtistid());
				mContext.startActivity(intent);
			}
		});
		return convertView;
	}

	class ViewHolder {

		TextView name, detail, time, zan, comment, share;
		DynamicHeightImageView pic;
		ImageView icon,auth;

	}
}
